﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.LabEvaluation
{
    public partial class ModifyLabEvaluation : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYSPGJG");
            strPageUserType.Add("mana");
            if (Request.QueryString["KindID"] != null && Request.QueryString["TitleID"] != null && Request.QueryString["LabID"]!=null && Request.QueryString["EvTimeID"]!=null)
            {
                if (!IsPostBack)
                {
                    int KindID, TitleID, LabID,EvTimeID;
                    if (int.TryParse(Request.QueryString["KindID"].ToString(), out KindID) && int.TryParse(Request.QueryString["TitleID"].ToString(), out TitleID) && int.TryParse(Request.QueryString["LabID"].ToString(), out LabID) && int.TryParse(Request.QueryString["EvTimeID"].ToString(), out EvTimeID))
                    {
                        LabIDHF.Value = LabID.ToString();
                        KindIDHF.Value = KindID.ToString();
                        TitleIDHF.Value = TitleID.ToString();
                        EvTimeIDHF.Value = EvTimeID.ToString();
                        BindData();
                    }
                    else
                    {
                        SaveBtn.Visible = false;
                        ERRLbl.Text = "系统异常，参数不合法！";
                        ERRLbl.Visible = true;
                    }
                }
            }
            else
            {
                SaveBtn.Visible = false;
                ERRLbl.Text = "系统异常，参数为空！";
                ERRLbl.Visible = true;
            }
        }

        //绑定
        protected void BindData()
        {
            LabDropDownList();//评估对象
            LabDDL.SelectedValue = LabIDHF.Value;
            LabDDL.Enabled = false;

            //评估标准类别
            KindDDL.DataSource = DictionaryDropdownlist("119");
            KindDDL.DataBind();
            KindDDL.SelectedValue = KindIDHF.Value;
            KindDDL.Enabled = false;

            //主题名
            TitleDropDownList(int.Parse(KindDDL.SelectedValue));
            TitleDDL.SelectedValue = TitleIDHF.Value;
            TitleDDL.Enabled = false;

            //日期
            DateDropDownList(int.Parse(TitleIDHF.Value), int.Parse(LabIDHF.Value));
            DateDDL.SelectedValue = EvTimeIDHF.Value;
            DateDDL.Enabled = false;

            //gridview
            LabEvalGV_DataBind(int.Parse(TitleIDHF.Value),int.Parse(LabIDHF.Value),int.Parse(EvTimeIDHF.Value));
        }

        //绑定GridView
        protected void LabEvalGV_DataBind(int TitleID,int LabID,int EvTimeID)
        {
            LabEvalGridView.DataSource = GetStandardByTitleID(TitleID,LabID.ToString(),EvTimeID);
            LabEvalGridView.DataBind();
        }

        //绑定评估实验室
        protected void LabDropDownList()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> labModel = new List<LabMS.Model.Lab>();

            labModel = lab.GetModelList("Lab_ParentID is null order by [ID] DESC");
            LabDDL.DataSource = labModel;
            LabDDL.DataBind();
        }

        //绑定评估日期
        protected void DateDropDownList(int ID, int LabID)
        {
            ExtendBLL.Standard sta = new ExtendBLL.Standard();
            DataSet ds = new DataSet();

            ds = sta.GetDateList(ID, LabID);
            DateDDL.DataSource = ds;
            DateDDL.DataBind();
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = "DClass='" + code + "'";
            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        //绑定主题
        protected void TitleDropDownList(int code)
        {
            LabMS.BLL.EvaluationTitle evalTitle = new LabMS.BLL.EvaluationTitle();
            List<LabMS.Model.EvaluationTitle> evalTitleModel = new List<LabMS.Model.EvaluationTitle>();

            evalTitleModel = evalTitle.GetModelList("Title_Category=" + code);
            TitleDDL.DataSource = evalTitleModel;
            TitleDDL.DataBind();
        }

        //根据主题ID获取评估标准信息
        protected DataSet GetStandardByTitleID(int TitleID, string strLabID,int EvTimeID)
        {
            ExtendBLL.Standard stan = new ExtendBLL.Standard();
            DataSet ds = new DataSet();
            try
            {
                ds = stan.GetSelfInfoByTitleID(TitleID, strLabID,EvTimeID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        //分页
        protected void LabEvalGV_OnPageChange(object sender, GridViewPageEventArgs e)
        {
            LabEvalGridView.PageIndex = e.NewPageIndex;
            LabEvalGV_DataBind(int.Parse(TitleDDL.SelectedValue),int.Parse(LabDDL.SelectedValue),int.Parse(DateDDL.SelectedValue));
        }

        //保存  
        protected void Save_OnClick(object sender, EventArgs e)
        {
            List<string[]> list = new List<string[]>();
            
            int count = LabEvalGridView.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                string[] str = new string[4];
                TextBox selftb = (TextBox)LabEvalGridView.Rows[i].FindControl("SelfEvalTBX");
                TextBox resulttb = (TextBox)LabEvalGridView.Rows[i].FindControl("EventTBX");
                RadioButton rbl = (RadioButton)LabEvalGridView.Rows[i].FindControl("IsRBL1");

                str[0] = selftb.Text.Trim();
                str[1] = resulttb.Text.Trim();
                if (rbl.Checked == true)
                {
                    str[2] = "1";
                }
                else
                {
                    str[2] = "0";
                }
                str[3] = LabEvalGridView.DataKeys[i]["EID"].ToString();
                list.Add(str);
            }

            ExtendBLL.Standard stan = new ExtendBLL.Standard();
            try
            {
                stan.UpdateSelfInfo(int.Parse(LabDDL.SelectedValue), list);
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('更新成功！');refreshParent();winclose()</script>");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
